package com.haiyuancloud.base.deal.ctr;


import com.alibaba.fastjson.JSON;
import com.haiyuancloud.base._enum.BaseDealCoreEnum;
import com.haiyuancloud.base.deal.core.chain.enter._enum.PageChainEnterInitEnumFactory;
import com.haiyuancloud.base.deal.core.chain.enter._enum.model.EnumModel;
import com.haiyuancloud.base.deal.core.select.list.service.SelectListTbDealEnumService;
import com.haiyuancloud.base.deal.core.select.list.service.SelectListTbDealPkgService;
import com.haiyuancloud.base.deal.core.select.list.service.SelectListTbDealTblJoinTblService;
import com.haiyuancloud.base.deal.core.select.list.service.SelectListTbDealVersionService;
import com.haiyuancloud.base.deal.core.service.DealDsToEnumService;
import com.haiyuancloud.base.deal.core.service.DealTblJoinTblToEnumService;
import com.haiyuancloud.base.deal.core.service.DictTblToEnumService;
import com.haiyuancloud.base.deal.lib.busi.SelectListDealTblColLibBusiService;
import com.haiyuancloud.base.ui.page.select.list.service.SelectListTbUiPageTplBlockUrlService;
import com.haiyuancloud.base.ui.page.select.list.service.SelectListTbUiPageTplPanelService;
import com.haiyuancloud.base.ui.tpl.select.list.service.SelectListTbUiTplBlockUrlService;
import com.haiyuancloud.base.ui.tpl.select.list.service.SelectListTbUiTplPanelService;
import com.haiyuancloud.db.core.model.DsDataModel;
import com.haiyuancloud.org.select.list.service.SelectTreeListTbOrgDeptService;
import com.haiyuancloud.org.select.list.service.SelectTreeListTbOrgDomainService;
import com.haiyuancloud.org.select.list.service.SelectTreeListTbOrgMenuService;
import com.haiyuancloud.org.select.list.service.SelectTreeListTbOrgRoleService;
import com.haiyuancloud.org.session.SessionUtil;
import com.haiyuancloud.quick.select.service.SelectTreeListTbQuickItemService;
import com.haiyuancloud.util.ObjectUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;


@Log4j
@Controller
@RequestMapping("/enum/ctr")
public class EnumCtr extends BaseCtr {

    @Resource
    private SelectListTbDealEnumService tbDealEnumService;

    @Resource
    private DealTblJoinTblToEnumService dealTblJoinTblToEnumService;

    @Resource
    private DictTblToEnumService dictTblToEnumService;


    @Resource
    private SelectListTbDealVersionService tbDealVersionService;

    @Resource
    private SelectListDealTblColLibBusiService selectListDealTblColLibBusiService;

    @Resource
    private SelectTreeListTbOrgDomainService selectTreeListTbOrgDomainService;

    @Resource
    private SelectTreeListTbOrgRoleService selectTreeListTbOrgRoleService;

    @Resource
    private SelectTreeListTbOrgMenuService selectTreeListTbOrgMenuService;

    @Resource
    private SelectTreeListTbOrgDeptService selectTreeListTbOrgDeptService;

    @Resource
    private SelectTreeListTbQuickItemService selectTreeListTbQuickItemService;

    @Resource
    private SelectListTbUiPageTplPanelService selectListTbUiPageTplPanelService;

    @Resource
    private SelectListTbUiTplPanelService selectListTbUiTplPanelService;

    @Resource
    private SelectListTbUiPageTplBlockUrlService selectListTbUiPageTplBlockUrlService;

    @Resource
    private SelectListTbUiTplBlockUrlService selectListTbUiTplBlockUrlService;

    @Resource
    private SelectListTbDealTblJoinTblService selectListTbDealTblJoinTblService;

    @Resource
    private DealDsToEnumService dealDsToEnumService;

    @Resource
    private PageChainEnterInitEnumFactory pageChainEnterInitEnumFactory;


   /***
     * 加载枚举列表
     * @param submitMap 提交参数
     */
    @RequestMapping(value = "/getEnumList")
    @ResponseBody
    public Map<String, Object> getEnumList(@RequestBody Map<String, Object> submitMap) {
        Map<String,Object> session = SessionUtil.initSession();
        DsDataModel dsDataModel = new DsDataModel();
        dsDataModel.setTENANT_DEAL_DS_ID(SessionUtil.getSessionTENANT_DEAL_DS_ID(session));
        dsDataModel.setTENANT_ORG_DS_ID(SessionUtil.getSessionTENANT_ORG_DS_ID(session));
        dsDataModel.setTENANT_UI_DS_ID(SessionUtil.getSessionTENANT_UI_DS_ID(session));
        dsDataModel.setTENANT_ID(ObjectUtil.toLong(SessionUtil.getSessionTENANT_ID(session)));
        dsDataModel.setTENANT_PARTITION(SessionUtil.getSessionPARTITION(session));
        Map<String, Object> returnData = this.getDefaultResultData();
        Map<String, Object> enumData = new HashMap<String, Object>();
        Map<String,Object> tmpData = pageChainEnterInitEnumFactory.getEnumData(dsDataModel,submitMap);
        enumData.putAll(tmpData);
        Iterator<String> iterator = submitMap.keySet().iterator();
        while (iterator.hasNext()) {
            List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
            String key = iterator.next();
            String value = ObjectUtil.toString(submitMap.get(key));
            if (key.equals("I_FILTER_SRC")) {
                //要改设计不走这里，入参配置环节解决这个问题
                String I_FILTER_SRC = ObjectUtil.toString(submitMap.get("I_FILTER_SRC"));
                if(BaseDealCoreEnum.I_FILTER_SRC.DEAL_COL.getValue().equals(I_FILTER_SRC)
                ) {
                    String S_DEAL_TBL_ID = ObjectUtil.toString(submitMap.get("S_DEAL_TBL_ID"));
                    String S_DEAL_TBL_VER = ObjectUtil.toString(submitMap.get("S_DEAL_TBL_VER"));
                    List<EnumModel> enumModelList  =dealTblJoinTblToEnumService.getDictTblEnumList(dsDataModel, S_DEAL_TBL_ID, S_DEAL_TBL_VER,null);
                    initEnumModelList(enumModelList, newList);
                }else if(BaseDealCoreEnum.I_FILTER_SRC.DICT_TBL.getValue().equals(I_FILTER_SRC)) {
                    String S_DS_ID = ObjectUtil.toString(submitMap.get("S_DS_ID"));
                    String S_TBL_ID = ObjectUtil.toString(submitMap.get("S_TBL_ID"));
                    List<EnumModel> enumModelList  = dictTblToEnumService.getDictTblEnumList(dsDataModel,S_DS_ID,S_TBL_ID);
                    initEnumModelList(enumModelList, newList);
                }else  if(BaseDealCoreEnum.I_FILTER_SRC.WHERE_SRC.getValue().equals(I_FILTER_SRC)){
                    String S_DEAL_TBL_ID = ObjectUtil.toString(submitMap.get("S_DEAL_TBL_ID"));
                    String S_DEAL_TBL_VER = ObjectUtil.toString(submitMap.get("S_DEAL_TBL_VER"));
                    List<EnumModel> enumModelList  = dealTblJoinTblToEnumService.getDictTblEnumList(dsDataModel, S_DEAL_TBL_ID, S_DEAL_TBL_VER,BaseDealCoreEnum.I_FILTER_SRC.WHERE_SRC.getValue());
                    initEnumModelList(enumModelList, newList);
                }else if(BaseDealCoreEnum.I_FILTER_SRC.WHERE_TARGET.getValue().equals(I_FILTER_SRC)){
                    String S_DEAL_TBL_ID = ObjectUtil.toString(submitMap.get("S_DEAL_TBL_ID"));
                    String S_DEAL_TBL_VER = ObjectUtil.toString(submitMap.get("S_DEAL_TBL_VER"));
                    List<EnumModel> enumModelList  = dealTblJoinTblToEnumService.getDictTblEnumList(dsDataModel, S_DEAL_TBL_ID, S_DEAL_TBL_VER,BaseDealCoreEnum.I_FILTER_SRC.WHERE_TARGET.getValue());
                    initEnumModelList(enumModelList, newList);
                }
                key = "S_TBL_ID";
            }
            if(newList.size() >0) {
                enumData.put(key, newList);
            }
        }

        returnData.put("enumData", enumData);
        return returnData;
    }

    private void initEnumModelList(List<EnumModel> enumModelList,List<Map<String,Object>> newList){
        for(EnumModel enumModel : enumModelList){
            Map<String, Object> emap = new HashMap<>();
            emap.put("text", enumModel.getLabel());//ok
            emap.put("value",enumModel.getValue());
            newList.add(emap);
        }
    }
}
